package com.example.mytest.Exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * @ClassName: ResultBody
 * @Description: 我们定义一下数据的传输格式
 * @Author: 230689
 * @Date: 2021/9/8 9:33
 * @Version: v1.0
 */
@Data
public class ResultBody {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private Object result;

    public ResultBody() {
    }

    public ResultBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    /**
     * 成功
     *
     * @return
     */
    public static ResultBody success() {
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static ResultBody success(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(CommonEnum.SUCCESS.getResultCode());
        rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(BaseErrorInfoInterface errorInfo) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }


    /**
     * 失败
     */
    public static ResultBody createFailedResult(Object data, BaseErrorInfoInterface errorInfo) {
        ResultBody resultBody = createResult(processData2Json(data), false, errorInfo);
        return resultBody;
    }

    public static ResultBody createResult(Object data, boolean success,  BaseErrorInfoInterface errorInfo) {
        ResultBody resultBody = new ResultBody();
        resultBody.code = success ? "0" : errorInfo.getResultCode();
        resultBody.message = errorInfo.getResultMsg();
        resultBody.result=processData2Json(data);
        return resultBody;
    }

    /**
     * 失败
     */
    public static ResultBody error( String message) {
        ResultBody rb = new ResultBody();
        rb.setCode("-1");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    private static Object processData2Json(Object data) {
        if (data == null) {
            data = new JSONObject();
        }

        JSONObject resultData = null;
        if (!(data instanceof Number) && !(data instanceof String) && !(data instanceof Object[])) {
            try {
                JSONObject object = JSON.parseObject(JSON.toJSONString(data));
                if (object != null) {
                    return data;
                }

                resultData = new JSONObject();
                resultData.put("value", data);
            } catch (Exception var3) {
                resultData = new JSONObject();
                resultData.put("value", data);
            }
        } else {
            resultData = new JSONObject();
            resultData.put("value", data);
        }

        return resultData;
    }
}
